package com.nacrtstudio.webgiftshop.pages;

import java.util.List;

import org.apache.tapestry5.annotations.*;
import org.apache.tapestry5.ioc.annotations.*;
import org.apache.tapestry5.services.Request;

import com.nacrtstudio.webgiftshop.facade.Controller;
import com.nacrtstudio.webgiftshop.model.shop.Gift;
import com.nacrtstudio.webgiftshop.model.shop.User;
import com.nacrtstudio.webgiftshop.model.shop.beans.CartBean;


/**
 * Start page of application webgiftshop.
 */
public class Index{
	@Inject
	private Request request;
	public void onActivate() {
		try {
			if (controller.getRequest()==null) {
	    		controller.setRequest(request);
			}
		} catch (Exception e) {
		}
	}
	
	@Inject
	private Controller controller;
	@Property
	private Gift gift;
	@InjectPage
	private GiftInfo giftInfo;
	@InjectPage
	private StatusPage statusPage;
	@InjectPage
	private Signin signinPage;
	
	public List<Gift> getAllGifts(){
		try {
			return controller.getAllGifts().subList(controller.getAllGifts().size()-3, controller.getAllGifts().size());
		} catch (Exception e) {
			return null;
		}
	}
	
	Object onActionFromShow(Long id){
		giftInfo.setGift(controller.findGift(id));
    	return giftInfo;
    }
	
	Object onActionFromAddToShoopingCard(Long id){
    	if (controller.getLoggedSignal()) {
    		return controller.addGiftToCart(id);
		}
    	signinPage.setMessage("Niste prijavljeni! Molimo Vas da se prijavite ili registrujete.");
    	return signinPage;
    }
}
